package org.dashboard.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdDataset;
import org.dashboard.bean.AdDatasource;
import org.dashboard.bean.AdFolder;
import org.dashboard.bean.AdSchema;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdDatasetMapper;
import org.dashboard.dao.AdDatasourceMapper;
import org.dashboard.dao.AdFolderMapper;
import org.dashboard.dao.AdSchemaMapper;
import org.dashboard.service.AdSchemaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = AdSchemaService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER, cacheNames = "schema")
public class AdSchemaServiceImpl implements AdSchemaService {
    private static final Logger logger = LoggerFactory.getLogger(AdSchemaServiceImpl.class);

    @Autowired
    private AdSchemaMapper adSchemaMapper;

    @Override
    @CacheEvict(allEntries = true)
    public int deleteByPrimaryKey(Integer schemaId) {
        return adSchemaMapper.deleteByPrimaryKey(schemaId);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int insert(AdSchema record) {
        adSchemaMapper.insert(record);
        return record.getSchemaId();
    }

    @Override
    @CacheEvict(allEntries = true)
    public int insertSelective(AdSchema record) {
        adSchemaMapper.insertSelective(record);
        return record.getSchemaId();
    }

    @Override
    @Cacheable(key = "'schemaId:' + #schemaId.toString()")
    public AdSchema selectByPrimaryKey(Integer schemaId) {
        return adSchemaMapper.selectByPrimaryKey(schemaId);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKeySelective(AdSchema record) {
        return adSchemaMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKeyWithBLOBs(AdSchema record) {
        return adSchemaMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    @CacheEvict(allEntries = true)
    public int updateByPrimaryKey(AdSchema record) {
        return adSchemaMapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public boolean deleteByPrimaryKeys(ArrayList<Integer> schemaIds) {
        for (Integer id : schemaIds) {
            adSchemaMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

    @Override
    @Cacheable(key = "'schemaCode:' + #schemaCode.toString()")
    public AdSchema selectAdSchemaByCode(String schemaCode) {
        return adSchemaMapper.selectAdSchemaByCode(schemaCode);
    }

    @Override
    public int checkSchemaCodeExist(String schemaCode, Integer schemaId) {
        return adSchemaMapper.checkSchemaCodeExist(schemaCode, schemaId);
    }

    @Override
    public List<AdSchema> selectAdSchemas() {
        return adSchemaMapper.selectAdSchemas();
    }

    @Override
    public PageInfo selectAdSchemasPaged(String criteria, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdSchema> adSchemas = adSchemaMapper.selectAdSchemasSearched(criteria);
        return new PageInfo<>(adSchemas);
    }

}
